package praktikumid.w09.p1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

/**
 * Listener for "draw oval" button.
 * @author Ago
 *
 */
public class DrawOvalListener implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("clicked");
		// getSource() returns the component where
		// the event happened, in our case JButton
		JButton b = (JButton) e.getSource();
		//System.out.println(e.getSource());
		// getParent() returns reference to the container component
		// in our case, JButton is inside the ButtonPanel
		ButtonPanel p = (ButtonPanel) b.getParent();
		
		// to get the frame, we need to get several levels
		// of parent. 
		// ButtonPanel is inside JPanel (framme's ContentPane)
		System.out.println(p.getParent());
		// JPanel is inside JLayeredPanel
		System.out.println(p.getParent().getParent());
		// JLayeredPane is inside JRootPane
		System.out.println(p.getParent().getParent().getParent());
		// JRootPane is inside JFrame
		MyFrame f = (MyFrame) p.getParent().getParent().getParent().getParent();
		System.out.println(f);

		// let's use getContentPane() to get content pane
		// from there, we find the second child component
		// (the first added element is with index 0 etc.)
		System.out.println(f.getContentPane().getComponent(1));
		DrawPanel dp = (DrawPanel) f.getContentPane().getComponent(1);
		dp.drawOval(100, 100);
	}

}
